package ven.spots;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class SpotManagerActivity extends Activity {
	private TextView macTV;
	private TextView rgbTV;
	private TextView stateTV;
	private ListView spotDataList;
	private Button   setStateBtn;
	private String iMAC;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spot_manager_layout);
        
        //Get extra values from the intent to later be thrown into appropriate textviews
        Intent intent = this.getIntent();
        int iState  = intent.getIntExtra("state", -1);
        int iRed    = intent.getIntExtra("red", 0);
        int iGreen  = intent.getIntExtra("green", 0);
        int iBlue   = intent.getIntExtra("blue", 0);
        
        //Decide what to do with the set state button...
        setStateBtn = (Button)this.findViewById(R.id.setStateBtn);
        setStateBtn.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick(View v) {
				DatabaseConnector2 dbc = new DatabaseConnector2();
				try {
					dbc.setState(iMAC, 255, 255, 255);
				} catch (IOException e) {
					Log.d("test", e.getMessage());
				}
			}
		});
        
        //Decide whether or not the button can be clicked base
        //on what state the sunspot is in (iState)
        if (setStateBtn != null) {
        	if (iState != -1 && iState != 2) {
        		//Spot is either offline or online(idle)
        		setStateBtn.setEnabled(true);
        		setStateBtn.setText(R.string.stateBtnEnabled);
        	} else {
        		//Spot is already flashing or error'd
        		setStateBtn.setEnabled(false);
        		setStateBtn.setText(R.string.stateBtnDisabled);
        	}
        }
        
        //Set the MAC, RGB, and State fields of the SpotManager activity
        macTV = (TextView)this.findViewById(R.id.smMACTV);
        rgbTV = (TextView)this.findViewById(R.id.smRGBTV);
        stateTV = (TextView)this.findViewById(R.id.smStateTV);
        
        if (macTV != null) {
        	macTV.setText("MAC  :  " + iMAC);
        }
        if (rgbTV != null) {
        	rgbTV.setText("RGB  :  (" + iRed + ", " + iGreen + ", " + iBlue + ")");
        }
        if (stateTV != null) {
        	stateTV.setText("State:  " + iState);
        }
        
        //Populate the ListView with data objects fetched by the database connector
        spotDataList = (ListView)this.findViewById(R.id.spotDataList);
        DatabaseConnector2 dbc = new DatabaseConnector2();
        ArrayList<SpotData> data = null;
        SpotDataListAdapter adapter;
        
        try {
        	data = dbc.getData(iMAC);
        } catch (Exception e) {
        	//lol
        	Log.d("test", e.getMessage());
        }
        
        if (data != null && spotDataList != null) {
        	adapter = new SpotDataListAdapter(this, R.layout.spot_data_row, data);
        	spotDataList.setAdapter(adapter);
        }
        
	}
	
    /**
     * Fills a ListView with TextViews representing SpotData object
     * @author Matthew Zizzi
     * Referenced: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
     */
	private class SpotDataListAdapter extends ArrayAdapter {
    	private ArrayList<SpotData> data;
    	
    	/**
    	 * Overridden constructor.  Sets arraylist of data to be used as a field var.
    	 * @param context
    	 * @param textViewResourceId
    	 * @param _data ArrayList<SpotData> of data to be used as a field var.
    	 */
		public SpotDataListAdapter(Context context,
								   int textViewResourceId,
								   ArrayList<SpotData> _data) {
			super(context, textViewResourceId, _data);
            this.data = _data;
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi;
				vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.spot_data_row, null);
			}
			
			SpotData sd = this.data.get(position);
			if (sd != null) {
				//Get instances of the needed textfields so we can add dynamic data
				TextView timeTV  = (TextView)v.findViewById(R.id.sdTimeTV);
				TextView stateTV = (TextView)v.findViewById(R.id.sdStateTV);
				TextView lightTV = (TextView)v.findViewById(R.id.sdLightTV);
				
				if (timeTV != null) {
					timeTV.setText("Time :  " + sd.getDate());
				}
				if (stateTV != null) {
					stateTV.setText("State:  " + sd.getState());
				}
				if (lightTV != null) {
					lightTV.setText("Light:  " + sd.getLight());
				}
				
			}//End null sd check
			return v;
		}//End getView
    }//End SpotDataListAdapter
}